/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcresponseparser;

import com.elcom.emocbc.cbcresponseparser.cbcresponse.CbcMessageResponse;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.CbcRequestType;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.CbcResponse;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.GetBscCellsResponse;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.GetBscsResponse;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.BscInfo;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.BscProperty;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.BscStatus;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.CbcMessage;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.CbcMessageStatus;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.CellInfo;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.ReturnValue;
import com.thoughtworks.xstream.XStream;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author GiangLT
 */
public class ResponseParser<T extends CbcResponse> {
    
    private XStream xStream;
    
    public ResponseParser() {
        xStream = new XStream();
        xStream.alias("cbcMessage", CbcMessage.class);
    }
    
    public String marshall(T cbcResponse) {
        xStream.alias("cbcResponse", cbcResponse.getClass());
        
        String requestType = cbcResponse.getRequest();
        if (requestType.equals(CbcRequestType.GET_BSCS)) {
            xStream.alias("bsc", BscInfo.class);
            xStream.alias("property", BscProperty.class);
            xStream.alias("status", BscStatus.class);
            xStream.addImplicitCollection(cbcResponse.getClass(), "bscInfos");
            xStream.useAttributeFor(BscProperty.class, "name");
            xStream.useAttributeFor(BscProperty.class, "value");
        } else if (requestType.equals(CbcRequestType.GET_BSC_CELLS)) {
            xStream.alias("cell", CellInfo.class);
            xStream.alias("returnValueGdg", ReturnValue.class);
            xStream.useAttributeFor(ReturnValue.class, "type");
            xStream.addImplicitCollection(ReturnValue.class, "cellInfos");
        }
        return xStream.toXML(cbcResponse);
    }
    
    public T unmarshall(String xmlResponse) {
        xStream.alias("cbcResponse", xmlResponse.getClass());
        return (T) xStream.fromXML(xmlResponse);
    }
    
    public static void main(String[] args) {
        ResponseParser<CbcMessageResponse> parser = new ResponseParser<CbcMessageResponse>();
        
        CbcMessageResponse cbcMessageResponse = new CbcMessageResponse();
        cbcMessageResponse.setRequest("sendmessage");
        cbcMessageResponse.setStatus("SUCCESS");
        
        CbcMessageStatus cbcMessageStatus = new CbcMessageStatus(0, "To be submited");
        CbcMessage cbcMessage = new CbcMessage("12", cbcMessageStatus);
        cbcMessageResponse.setCbcMessage(cbcMessage);
        
        System.out.println(parser.marshall(cbcMessageResponse));
        
        
        BscInfo bscInfo = new BscInfo();
        List<BscProperty> bscProperties = new ArrayList<BscProperty>();
        bscProperties.add(new BscProperty("name", "BSCGM43"));
        bscProperties.add(new BscProperty("bscid", "43"));
        bscProperties.add(new BscProperty("version", "BR55"));
        bscProperties.add(new BscProperty("primaryport", "234"));
        bscProperties.add(new BscProperty("vendor", "NOKIA"));
        bscProperties.add(new BscProperty("protocol", "CLNS"));
        bscProperties.add(new BscProperty("bscprimaryaddress", "0004238742349876"));
        bscInfo.setProperties(bscProperties);
        
        BscStatus bscStatus = new BscStatus();
        bscStatus.setDescription("ACTIVATED");
        bscStatus.setStatus("ACTIVATED");
        bscStatus.setReconnectionNumber(0);
        bscStatus.setInitializationTime("10");
        bscInfo.setStatus(bscStatus);
        
        List<BscInfo> bscInfos = new ArrayList<BscInfo>();
        bscInfos.add(bscInfo);
        GetBscsResponse getBscsResponse = new GetBscsResponse();
        getBscsResponse.setBscInfos(bscInfos);
        getBscsResponse.setStatus("SUCCESS");
        System.out.println(parser.marshall(getBscsResponse));
        
        CellInfo cellInfo = new CellInfo();
        cellInfo.setBscId("1");
        cellInfo.setCi("23435");
        cellInfo.setGatewayId("gw9034");
        cellInfo.setId("34");
        cellInfo.setLac("lac1234");
        cellInfo.setMcc("4");
        cellInfo.setMnc("6");
        cellInfo.setName("Cell23435");
        cellInfo.setStatus("ACTIVE");
        cellInfo.setStatusDescription("ACTIVE");
        List<CellInfo> cellInfos = new ArrayList<CellInfo>();
        cellInfos.add(cellInfo);
        ReturnValue cellReturnValue = new ReturnValue("list", cellInfos);
        GetBscCellsResponse getBscCellsResponse = new GetBscCellsResponse();
        getBscCellsResponse.setCellReturnValue(cellReturnValue);
        getBscCellsResponse.setStatus("SUCCESS");
        System.out.println(parser.marshall(getBscCellsResponse));
    }
}
